layui.define(function (exports) {
    let tool = {
        /**
         * 时间戳转换为日期
         * @param timestamp // 时间戳
         * @param format // 日期格式
         * @returns string
         */
        timestampToTime: function(timestamp, format = 'Y-m-d H:i:s') {
            if (!tool.isNumber(timestamp) === true) {
				return timestamp;
            }
			let date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
			let Y = date.getFullYear();
			let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
			let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate());
			let h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours());
			let m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes());
			let s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
			if (format === 'Ymd') {
			    return Y + M + D;
			} else if (format === 'Y-m-d') {
			    return Y + '-' + M + '-' + D;
			} else if (format === 'Y-m-d H:i:s') {
			    return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s;
			} else if (format === 'Y/m/d') {
			    return Y + '/' + M + '/' + D;
			} else if (format === 'Y/m/d H:i:s') {
			    return Y + '/' + M + '/' + D + ' ' + h + ':' + m + ':' + s;
			} else if (format === 'H:i:s') {
			    return h + ':' + m + ':' + s;
			} else if (format === 'H:i') {
			    return h + ':' + m;
			} else if (format === 'H') {
			    return h;
			} else if (format === 'i') {
			    return m;
			} else if (format === 's') {
			    return s;
			}
        }
        /**
         * 日期转换为时间戳
         * @param date_str
         * @returns number
         */
        ,dateToTimestamp: function(date_str) {
            const date = new Date(date_str);
            let time = Date.parse(date);
            // 除1000得到时间戳
            return time / 1000;
        }
        /**
         * 判断是否为数字(包含字符串数字)
         * @param num
         * @returns boolean
         */
        ,isNumber: function(num = '') {
            if (num){
                return /^[0-9]+.?[0-9]*$/.test(num);
            }else{
                return false;
            }
        }
    }
    //接口输出
    exports('tool', tool)
})
